//
//  MineVc.swift
//  Pods
//
//  Created by eme on 2017/3/23.
//
//
/*
 owner:gyh
 update:2017年03月23日11:54:33
 info: 我的
 */
import UIKit
import UtilCore
import RxSwift
import RickonCore
import Kingfisher
import BrandonCore

class MineVc: Base_Vc {
    
    /****************************Storyboard UI设置区域****************************/
    @IBOutlet weak var Mine_TableView: UITableView!    ///表
    
//    @IBOutlet weak var LoginButton: UIButton!
    /*----------------------------   自定义属性区域    ----------------------------*/
    var OrderNum:OrderNumVm! = nil
    
    var orderNum_Model:OrderNumModel! = nil
    
    var LoginUserVm:GetLoginUserVm?
    
    var Loginvm:LoginVm?
    
    /****************************Storyboard 绑定方法区域****************************/
    @IBAction func aboutOrderClicked(_ sender: UIButton) {
        toOrderList(sender.tag)
    }
    
    @IBAction func MessageBtnClicked(_ sender: UIButton) {
//        self.Loginvm?.getLoginUser.onNext("213")
        _ = "message".openURL()
//        _ = "Category_Vc".openURL()
//        self.tabBarController?.tabBar.isHidden = true
    }
    @IBAction func rz_Clicked(_ sender: UIButton) {
        if case AppDataState.notAudit = AppData.appState{
            //跳转提交认证资料界面
            _ = "authinfopagevc".openURL(["spid":AppData.loginedUser?.glShop.spid ?? ""])
        }else{
            _ = "certification".openURL()
        }
        
    }
    
    @IBAction func login(_ sender: UIButton) {
        _ = "login".openURL()
    }
    /**************************** 以下是方法区域 ****************************/
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = Theme.sansa.order.normal
        
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        self.navigationController?.setNavigationBarHidden(true, animated: false)
        self.tabBarController?.tabBar.isHidden = false
        if AppData.loginedUser != nil{
            self.OrderNum.changeMessage.onNext()
            self.LoginUserVm?.getLoginUser.onNext()
            
        }
        self.Mine_TableView.reloadData()
        
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        
        self.navigationController?.setNavigationBarHidden(false, animated: true)
        
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        
    }
    /**
     界面基础设置
     */
    override func setupUI() {
        /**
         *  自定义 导航栏左侧 返回按钮
         */
//        self.customLeftBarButtonItem()
        self.setupTableView()
//        
    }
    func toOrderList(_ index:Int){
        if AppData.loginedUser != nil{
            _ = "order".openURL(["index":"\(index)"])
        }else{
            _ = "login".openURL()
        }
        
        
        
//        let order = BaseOrderVc()
//        order.Orderindex = index
//        self.navigationController?.pushViewController(order, animated: true)
    }
    
    /**
     绑定到viewmodel 设置
     */
    override func bindToViewModel(){
        
        self.OrderNum = OrderNumVm()
        self.OrderNum.backMessage.asObserver().subscribe(onNext: { (result) in
            
            self.orderNum_Model = result
            self.Mine_TableView.reloadData()
            
        }).addDisposableTo(disposeBag)
        
//        self.OrderNum.error.bindTo(self.view.rx_error).addDisposableTo(disposeBag)
        
        self.LoginUserVm = GetLoginUserVm()
        
        self.LoginUserVm?.backData.asObserver().subscribe(onNext:{ user in
            
            self.Mine_TableView.reloadData()
            
        }).addDisposableTo(disposeBag)
        
        self.Loginvm = LoginVm()
        

    }
    
    func setupTableView(){
        self.Mine_TableView.showsVerticalScrollIndicator = false
        Mine_TableView.delegate = self
        Mine_TableView.dataSource = self
        if Mine_TableView.frame.height > UIScreen.main.bounds.height - 44
        {
            Mine_TableView.isScrollEnabled = true
        }else{
            Mine_TableView.isScrollEnabled = false
        }
        Mine_TableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
    }
    /*
     点击事件
     */
    func selectIcon(){
        
        _ = "storemessage".openURL()
//        self.tabBarController?.tabBar.isHidden = true
    }
}
extension MineVc:UITableViewDelegate,UITableViewDataSource{
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 11
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        if indexPath.row == 0{
            let headerCell = tableView.dequeueReusableCell(withIdentifier: "header") as! Mine_header_TableViewCell
            headerCell.item = self.orderNum_Model
            if let user = AppData.loginedUser{

                headerCell.header_ImageView.kf.setImage(with:URL(string: user.userface),placeholder: UIImage.init(named: "Mine_default_Image", in: SansaCore.bundle, compatibleWith: nil), options:[.transition(ImageTransition.fade(1.2))])
                
                if user.nickname != ""{
                    
                    headerCell.nameLabel.text = user.nickname
                }else{
                    headerCell.nameLabel.text = user.thrid
                }
                
            }
            headerCell.header_ImageView.isHidden = AppData.loginedUser == nil ? true:false
            headerCell.nameLabel.isHidden = AppData.loginedUser == nil ? true:false
            headerCell.loginButton.isHidden = AppData.loginedUser == nil ? false:true
            
            headerCell.selectionStyle = .none
            headerCell.header_ImageView.isUserInteractionEnabled = true
            let userIconActionGR = UITapGestureRecognizer()
            userIconActionGR.addTarget(self, action: #selector(MineVc.selectIcon))
            headerCell.header_ImageView.addGestureRecognizer(userIconActionGR)
            return headerCell
        }else if indexPath.row == 1{
            
            let cell = tableView.dequeueReusableCell(withIdentifier: "rzcell", for: indexPath) as! RZ_TableViewCell
            cell.status = ""
            return cell
            
        }else if indexPath.row != 3 {
            let cell = tableView.dequeueReusableCell(withIdentifier: "help") as! Help_TableViewCell
            let customCell = tableView.dequeueReusableCell(withIdentifier: "basisCell") as! MineTableViewCell
            customCell.title_Label.text = ["","","我的订单","","优惠券","快速补货","我的收藏","足迹","收货地址","设置","帮助与客服"][indexPath.row]
            if indexPath.row == 10{
                return cell
            }else{
                return customCell
            }
        }else{
            
            let cell = tableView.dequeueReusableCell(withIdentifier: "otherCell") as! PaymentTableViewCell
            cell.item = self.orderNum_Model
            
            return cell
        }
        
        
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if indexPath.row == 3
        {
            return 188/2
        }
        else if indexPath.row == 0
        {
            return self.view.frame.width/(750/379)
        }
        else if indexPath.row == 10{
            return 54
        }else if indexPath.row == 1{
            if AppData.loginedUser != nil{
                if case .tourists = AppData.appState {
                    return 0.01
                }else if case .audited = AppData.appState{
                    return 0.01
                }else{
                    return 55
                }
            }else{
                return 0.01
            }
        }else{
            return 45
        }
        
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print(indexPath.row)
        if indexPath.row != 0{
            self.perform(#selector(MineVc.deselect), with: nil, afterDelay: 0.1)
            if AppData.loginedUser != nil{
                switch indexPath.row {
                case 2:
                    toOrderList(0)
                case 9:
                    _ = "setup".openURL()
                case 4:
                    _ = "coupons".openURL()
                case 5:
                    _ = "replenishment".openURL()
                case 6:
                    _ = "collect_vc".openURL()
                case 7:
                    _ = "footer".openURL()
                case 8:
                    _ = "magagenewaddress".openURL()
                case 10:
                    _ = "https://static.emeker.com/help/3/index.html".openURL()
                default:
                    break
                }
            }else if indexPath.row == 10{
                
                _ = "https://static.emeker.com/help/3/index.html".openURL()
                
            }else{
                _ = "login".openURL()
            }
        }
    }
    func deselect(){
        
        if let indexrow = self.Mine_TableView.indexPathForSelectedRow{
            self.Mine_TableView.deselectRow(at: indexrow, animated: true)
        }else{
            
        }
        
    }

}

